package com.teamwin.hdmi.donate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.UnknownHostException;

class SocketListenerThread extends Thread implements Runnable {
	private boolean running = false;
	private SocketManager parent;
	
	SocketListenerThread(SocketManager parent) {
		this.parent = parent;
	}
	
	public void run() {
//		System.out.println("HDMwIn::Thread::run() - thread starting!");
		try {
			BufferedReader br = null;
			try {
				br = new BufferedReader(new InputStreamReader(this.parent.getSocket().getInputStream()));
				this.setRunning(true);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			String line = null;
			while (this.isRunning() && (line = br.readLine()) != null) {
				this.parent.setCommandResult(line);
				this.parent.setHasCommandResult(true);
//				System.out.println("Received msg: " + line);
				this.setRunning(false);
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
//		System.out.println("HDMwIn::Thread::run() - thread quitting!");
	}

	public void setRunning(boolean running) {
		this.running = running;
	}

	public boolean isRunning() {
		return running;
	}
}